﻿using DemountCIM.Framework.DataBase;
using DemountCIM.Framework.DataBase.Table;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DemountCIM.ViewHelper
{
    public class GlobalViewModel : BindableBase
    {
        private static GlobalViewModel global;
        private DataBaseContainer database;
        private TbUser curUser;
        private bool isAdmin;
        private bool isEngineer;
        private bool isOperator;



        public GlobalViewModel()
        {
            database = DataBaseContainer.GetInstance();
            curUser = new TbUser();

            Task.Factory.StartNew(() => LiveUpdate());
        }

        /// <summary>
        /// 获取全局对象
        /// </summary>
        /// <returns></returns>
        public static GlobalViewModel GetInstance()
        {
            if (null == global)
            {
                global = new GlobalViewModel();
            }
            return global;
        }



        public bool IsAdmin
        {
            get { return isAdmin; }
            set
            {
                SetProperty(ref isAdmin, value);
            }
        }

        public bool IsEngineer
        {
            get { return isEngineer; }
            set
            {
                SetProperty(ref isEngineer, value);
            }
        }

        public bool IsOperator
        {
            get { return isOperator; }
            set
            {
                SetProperty(ref isOperator, value);
            }
        }

        /// <summary>
        /// 实时更新线程
        /// </summary>
        public void LiveUpdate()
        {
            while (true)
            {
                database.GetUserRepository().GetCurUser(ref curUser);

                IsAdmin = curUser.Level <= UserLevel.管理员;
                IsEngineer = curUser.Level <= UserLevel.工程师;
                IsOperator = curUser.Level <= UserLevel.操作工;


                Thread.Sleep(1000);
            }
        }
    }
}
